package com.fhz.day007.coroutine

import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis

/**
 * 时间:2021/10/27
 * @author msfeng
 * 简述: TODO
 */
fun main() {


    val job = GlobalScope.launch {

    }

    job.cancel()





    runBlocking {
        val time = measureTimeMillis {
            val one = doSomethingUsefulOne()
            val two = doSomethingUsefulTwo()
            println("The answer is ${one + two}")
        }
        println(time)


        val time2 = measureTimeMillis {

            val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
            val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
            // 执行一些计算
            one.start() // 启动第一个
            two.start() // 启动第二个
            println("The answer is ${one.await() + two.await()}")
        }
        println("Completed in $time2 ms") //1秒


    }
}
 suspend fun doSomethingUsefulOne() : Int{
    delay(1000)
    return 1
}

suspend fun doSomethingUsefulTwo() : Int{
    delay(1000)
    return 1
}